package com.tjtt.tdm.codeReview.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.codehaus.groovy.runtime.NullObject;
import org.ietf.jgss.Oid;

import com.ibatis.dao.client.template.OjbBrokerDaoTemplate;
import com.tjtt.common.utils.DateUtilsAll;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.codeReview.dao.IItemDao;
import com.tjtt.tdm.codeReview.service.IItemService;

public class ItemServiceImpl implements IItemService{

	private IItemDao itemDao;

	/**
	 * 
	 * 描述：根据taskId查询样品列表
	 * @author wangxd
	 * @date Jun 30, 2015 4:20:46 PM
	 * @return PageResult
	 * @throw
	 */
	public PageResult querySampleByTaskId(Map filterMap) 
	{
		return itemDao.querySampleByTaskId(filterMap);
	}
	/**
	 * 
	 * 描述：根据taskId查询整车样品列表
	 * @author wangxj
	 * @date Jun 30, 2015 4:20:46 PM
	 * @return PageResult
	 * @throw
	 */
	public PageResult queryZCSampleByTaskId(Map filterMap){
		return itemDao.queryZCSampleByTaskId(filterMap);
	}
	/**
	 * 
	 * 描述：根据taskId查询检测项目列表
	 * @author wangxd
	 * @date Jun 30, 2015 4:20:46 PM
	 * @return PageResult
	 * @throw
	 */
	public PageResult queryItemByTaskId(Map filterMap)
	{
		return itemDao.queryItemByTaskId(filterMap);
	}
	
	/**
	 * 
	 * 描述：选择检测项目的左侧列表
	 * @author wangxd
	 * @date Jul 3, 2015 4:09:25 PM
	 * @return PageResult
	 * @throw
	 */
	public PageResult queryItemFromTable(Map filterMap) 
	{
		return itemDao.queryItemFromTable(filterMap);
	}
	/**
	 * 
	 * 描述：根据检测项目查询对应的检测标准
	 * @author wangxd
	 * @date Jul 3, 2015 4:29:07 PM
	 * @return PageResult
	 * @throw
	 */
	public PageResult queryItemBiaozhunByItemId(Map filterMap)
	{
		return itemDao.queryItemBiaozhunByItemId(filterMap);
	}
	/**
	 * 
	 * 描述：保存检测项目和检测标准
	 * @author wangxd
	 * @date Jul 6, 2015 1:44:03 PM
	 * @return void
	 * @throw
	 */
	public void saveFlowProject(String ids,String taskId){
		if(!"".equals(ids)){
			String[] allIds =ids.split("@");
			for(int i=0;i<allIds.length;i++){
				if(null!=allIds){
					String[] id = allIds[i].split("_");     //检测项目ID and标准ID
					if(null!=id[0]){
						
							//根据检测项目ID查询检测项目信息
							String itemName ="";      //检测项目
							String jiancedanwei ="" ; //检测单位
							String gongsidaim = "";   //检测单位代码
							String jiancedidian ="";  //检测地点
							String jiancebumen = "";  //检测部门
							String jiancelieb ="";    //检测类别
							String jiancefeiyong =""; //检测费用
							String biaozhun = ""; //检测标准
							Long bumenId = null; //检测部门ID
							String danweiName =""; //检测单位名称
							
							//查询检测项目基本信息
							String querySql = " select t.t_itemname,t.t_jiancedanwei,t.t_gongsidaima,t.t_jiancedidian,t.t_jiancebumen,t.t_jiaceleibie,t.t_jiancebumen_id,t.t_jiancedanwei_name from gwm_item t left join gwm_item t1 on  t.t_itemname = t1.t_itemname  and t.t_jiancedanwei = t1.t_jiancedanwei and t.t_jiaceleibie = t1.t_jiaceleibie and t.t_jiancebumen = t1.t_jiancebumen where t1.id = "+id[0];
							List queryList = itemDao.getDatabySql(querySql);
							if(null!=queryList&&queryList.size()>0&&null!=queryList.get(0)){
								Object[] obj = (Object[])queryList.get(0);
								itemName = obj[0]!=null?obj[0].toString():"";
								jiancedanwei = obj[1]!=null?obj[1].toString():"";
								gongsidaim = obj[2]!=null?obj[2].toString():"";
								jiancedidian = 	obj[3]!=null?obj[3].toString():"";	
								jiancebumen = 	obj[4]!=null?obj[4].toString():"";	
								jiancelieb = 	obj[5]!=null?obj[5].toString():"";	
								bumenId = 	obj[6]!=null?Long.parseLong(obj[6].toString()):null;	
								danweiName = 	obj[7]!=null?obj[7].toString():"";	
							}
							if(id.length>1){
								//查询检测标准
								String querySql1 ="select  wmsys.wm_concat(distinct t.t_jiancebiaozhun) from gwm_item t where t.id in ("+id[1]+") and t.t_jiancebiaozhun is not null";
								List querylist1 = itemDao.getDatabySql(querySql1);
								if(null!=querylist1&&querylist1.size()>0&&null!=querylist1.get(0)){
									biaozhun = querylist1.get(0).toString();
								}
								//查询检测feiyong 
								String querySql2 ="select  t.t_jiancemoney from gwm_item t where t.id in("+id[1]+") and t.t_jiancemoney is not null";
								List querylist2 = itemDao.getDatabySql(querySql2);
								if(null!=querylist2&&querylist2.size()>0&&null!=querylist2.get(0)){
									jiancefeiyong = querylist2.get(0).toString();
								}
							}
							//查询检测参数
							String querySql3 ="select  distinct t.t_jiancecanshu from gwm_item t left join gwm_item t1 on  t.t_itemname = t1.t_itemname  and t.t_jiancedanwei = t1.t_jiancedanwei and t.t_jiaceleibie = t1.t_jiaceleibie and t.t_jiancebumen = t1.t_jiancebumen where t1.id = "+id[0]+" and t.t_jiancecanshu is not null";
							List querylist3 = itemDao.getDatabySql(querySql3);
							if(null!=querylist3&&querylist3.size()>0){
								for(int j=0;j<querylist3.size();j++){
									String canshu = querylist3.get(j).toString();
									
									//保存检测项目
									String itemSql = "insert into gwm_item_flow " +
											         "(id, t_biaozhunweituo, t_baogaotype, t_bgyaoqiu, t_annex, t_itemid, t_itemtype, t_taskid,t_itemname,t_jiancedanwei,t_gongsidaima,t_jiancedidian,t_jiancebumen,t_jiaceleibie,t_jiancemoney,t_jiancecanshu,t_jiancebiaozhun,t_jiancebumen_id,t_jiancedanwei_name)  values  " +
											         "(SEQ_GWM_ITEM_FLOW.Nextval, '否', '出具检测报告', '中文', null,"+id[0]+", 0, "+taskId+",'"+itemName+"','"+jiancedanwei+"','"+gongsidaim+"','"+jiancedidian+"','"+jiancebumen+"','"+jiancelieb+"','"+jiancefeiyong+"','"+canshu+"','"+biaozhun+"',"+bumenId+",'"+danweiName+"')";
									itemDao.executeSql(itemSql);
								}
							
						  }else{
							   //检测参数等于检测项目
								String itemSql = "insert into gwm_item_flow " +
										         "(id, t_biaozhunweituo, t_baogaotype, t_bgyaoqiu, t_annex, t_itemid, t_itemtype, t_taskid,t_itemname,t_jiancedanwei,t_gongsidaima,t_jiancedidian,t_jiancebumen,t_jiaceleibie,t_jiancemoney,t_jiancecanshu,t_jiancebiaozhun,t_jiancebumen_id,t_jiancedanwei_name)  values  " +
										         "(SEQ_GWM_ITEM_FLOW.Nextval, '否', '出具检测报告', '中文', null,"+id[0]+", 0, "+taskId+",'"+itemName+"','"+jiancedanwei+"','"+gongsidaim+"','"+jiancedidian+"','"+jiancebumen+"','"+jiancelieb+"','"+jiancefeiyong+"','"+itemName+"','"+biaozhun+"',"+bumenId+",'"+danweiName+"')";
								itemDao.executeSql(itemSql);
						  }
					}
				}
			}
		}
	}
	/**
	 * 
	 * 描述：删除已选的检测项目
	 * @author wangxd
	 * @date Jul 6, 2015 3:37:07 PM
	 * @return void
	 * @throw
	 */
	public void deleteFlowProject(String taskId,String itemFlowIds){
		String itemSql = " delete from gwm_item_flow t where t.t_taskid = "+taskId+"  and t.t_itemid in ("+itemFlowIds+") and t.t_itemtype= 0  ";
		itemDao.executeSql(itemSql);
	}
	/**
	 * 
	 * 描述：保存检测项目列表
	 * @author wangxd
	 * @date Jul 7, 2015 2:40:00 PM
	 * @return void
	 * @throw
	 */
	public void saveItemGrid(String flowtaskid,String msg){
		String[] allMsg = msg.split(",");
		for(int i=0;i<allMsg.length;i++){
			String[] arr = allMsg[i].split("@");
			String id = arr[0];
			String value = arr[1].replace("#",",");
			String sql = "update gwm_item_flow t set "+value +" where t.t_taskid = "+flowtaskid+" and t.t_itemid = "+id;
			itemDao.executeSql(sql);
		}
	}
	/**
	 * 
	 * 描述：保存其他检测项目列表
	 * @author wangxd
	 * @date Jul 7, 2015 2:40:00 PM
	 * @return void
	 * @throw
	 */
	public void saveGridOtherItem(String msg,String flowtaskId){
		String[] allMsg = msg.split(",");
		//删除已有的数据
		String deleteSql =" delete from gwm_item_flow t where t.t_itemtype=1 and t.t_taskid="+flowtaskId;
		itemDao.executeSql(deleteSql);
		for(int i=0;i<allMsg.length;i++){
			//保存列表的数据
			String  values = allMsg[i].toString();
			values = values.replace("#",",");
			String sql = "insert into gwm_item_flow  (ID,T_ITEMNAME,T_JIANCECANSHU,T_BIAOZHUNWEITUO,T_BAOGAOTYPE,T_BGYAOQIU,T_ITEMTYPE,T_TASKID,T_ITEMID) values  (SEQ_GWM_ITEM_FLOW.Nextval,"+values+",1,"+flowtaskId+",SEQ_GWM_ITEM.Nextval)";
			itemDao.executeSql(sql);
		}
	}
	/**
	 * 
	 * 描述：其他检测项目存入到检测项目表中
	 * @author wangxd
	 * @date Jul 7, 2015 6:05:01 PM
	 * @return void
	 * @throw
	 */
	public void saveItemIntoGWMItem(String itemName,String deptId){
		String[] allNames = itemName.split(",");
		for(int i=0;i<allNames.length;i++){
			String sql = "insert into gwm_item (id,T_ITEMNAME,T_JIANCEDANWEI) values(SEQ_GWM_ITEM.Nextval,'"+allNames[i]+"','"+deptId+"')";
		    itemDao.executeSql(sql);
		}
	}
	/**
	 * 
	 * 描述：常规检测流程--->项目分配中 样品列表
	 * @author wangxd
	 * @date Jul 14, 2015 4:17:57 PM
	 * @return void
	 * @throw
	 */
	public PageResult queryItemFenpeiSampleno(Map filterMap) 
	{
		return itemDao.queryItemFenpeiSampleno(filterMap);
	}
	/**
	 * 
	 * 描述：常规检测流程--->项目分配中 样品列表
	 * @author wangxd
	 * @date Jul 14, 2015 4:17:57 PM
	 * @return void
	 * @throw
	 */
	public PageResult queryItemFenpeiItem(Map filterMap)
	{
		return itemDao.queryItemFenpeiItem(filterMap);
	}
	/**
	 * 
	 * 描述：常规检测流程--->项目分配中 样品/项目列表
	 * @author wangxd
	 * @date Jul 14, 2015 4:17:57 PM
	 * @return void
	 * @throw
	 */
	public PageResult queryItemSampleByTaskId(Map filterMap) 
	{
	    return itemDao.queryItemSampleByTaskId(filterMap);
	}
	
	/**
	 * 
	 * 描述：分配样品、项目、科室领导
	 * @author wangxd
	 * @date Jul 14, 2015 5:50:46 PM
	 * @return void
	 * @throw
	 */
	public void fenpeiItemAndSample(String sampleIds,String itemIds,String username,String userid,String deptId,String deptName,String taskId,String flowtaskinfoid,String flag ){
		if(!"".equals(sampleIds)&&!"".equals(itemIds)){		
			//插入新数据
			String lastItemFlowIds = itemIds;
			String[] allSampleIds = sampleIds.split(",");
			String[] allItemIds = itemIds.split(",");
			int l=allSampleIds.length;
			//根据itemIds 查询gwm_item_flow 中所有的id
			String sql = "select wmsys.wm_concat(t.id) from gwm_item_flow t where t.t_itemid in ("+itemIds+") and t.t_taskid = "+taskId;
			List list = itemDao.getDatabySql(sql);
			if(null!=list&&list.size()>0&&null!=list.get(0)){
				lastItemFlowIds = list.get(0).toString();
			}
			itemIds = lastItemFlowIds;
			String insertSql="insert into gwm_item_sample(id,t_samplenoid,t_sampleid, t_samplename, t_sampleno, t_itemid, t_itemname, t_jiaceleibie, "+
                          " t_deptid, t_deptleaderid, t_deptleadername, t_taskid, t_deptname,t_taskinfoid,ADDTIME,T_JIANCEMONEY,T_JIANCECANSHU,T_JIANCEBIAOZHUN,T_GMW_ITEM_ID) "+
                          " select SEQ_GWM_ITEM_SAMPLE.Nextval, yp.id, yp.t_sampleid,s.t_samplename,yp.t_sampleno , t.id, t.t_itemname, t.t_jiaceleibie,"+deptId+","+userid+",'"+username+"',"+taskId+",'"+deptName+"',"+flowtaskinfoid+",'"+DateUtilsAll.dateToStringShort(new Date())+"',t.t_jiancemoney,t.t_jiancecanshu,t.t_jiancebiaozhun,t.t_itemid"+
                          // " '1', '5286','管理员','27645','长城汽车股份有限公司','15704', sysdate "+
                          " from gwm_item_flow t , gwm_samplenoflow yp left join gwm_sample s  on yp.t_sampleid = s.id "+ 
                          " where  yp.id in ( " +sampleIds+ ") and t.id in ("+itemIds +") "+
                          " and not EXISTS(select * from gwm_item_sample gis where gis.t_itemid = t.id  and gis.t_samplenoid = yp.id)";
			itemDao.executeSql(insertSql);
			
			//重新修改费用总计字段
			String itemMoneySql = "select sum(a.t_jiancemoney) from ( select distinct t.t_sampleno,t.t_gmw_item_id,t.t_jiancemoney  from gwm_item_sample t where t.t_taskid = "+taskId+" and t.t_jiancemoney is not null) a";
			List itemMoneyList = itemDao.getDatabySql(itemMoneySql);
			if(null!=itemMoneyList&&itemMoneyList.size()>0&&null!=itemMoneyList.get(0)){
				String  itemMoney = itemMoneyList.get(0).toString();
				if("0".equals(flag)){
					String updateSql = "update gwm_changgijiance t  set t.t_feiyongzongji = "+itemMoney+" where t.id = (select f.main_id from flow_task f where f.id="+taskId+")";
					itemDao.executeSql(updateSql);
				}else{
					String updateSql = "update GWM_WEITUODAN t  set t.FEIYONGZONGJI = "+itemMoney+" where t.id = (select f.main_id from flow_task f where f.id="+taskId+")";
					itemDao.executeSql(updateSql);
				}
				
			}
			
			
//			for(int i=0;i<allSampleIds.length;i++){
//				String sampleName ="";   //样品名称
//				String sampleNo = "";    //样品编号
//				String sampleId = "";  //样品ID
//				String sampleNoId = allSampleIds[i];
//				//查询样品名称
//				String sampleSql = "select  s.t_samplename,t.t_sampleno ,t.t_sampleid from gwm_samplenoflow t  left join gwm_sample s  on t.t_sampleid = s.id where  t.id="+sampleNoId;
//				List sampleList = itemDao.getDatabySql(sampleSql);
//				if(null!=sampleList&&sampleList.size()>0&&null!=sampleList.get(0)){
//					Object[] objects = (Object[])sampleList.get(0);
//					if(null!=objects[0]){
//						sampleName = objects[0].toString();
//					}
//					if(null!=objects[1]){
//						sampleNo = objects[1].toString();
//					}
//					if(null!=objects[2]){
//						sampleId = objects[2].toString();
//					}
//				}
//				
//				for(int j=0;j<allItemIds.length;j++){
//					String itemName ="";         //检测项目名称
//					String jiancebiaozhun ="";   //检测标准
//					String jianceleibie ="";
//					String itemId = allItemIds[j];
//					
//					//判断样品和项目是否已经存在
//					String checkSql = "select t.id from gwm_item_sample t where t.t_sampleid="+sampleId +" and t.t_sampleno='"+sampleNo +"' and t.t_itemid="+itemId +" and t.t_taskid="+taskId;
//					List checkList = itemDao.getDatabySql(checkSql);
//					if(null!=checkList&&checkList.size()>0&&null!=checkList.get(0)){
//						
//					}else{
//						//根据样品ID和检测项目ID判断gwm_item_sample中是否存在
//						String querySql = " select t.id from gwm_item_sample t where t.t_taskid ='"+taskId+"' and t.t_itemid="+itemId+" and t.t_samplenoid="+sampleNoId;
//						List queryList = itemDao.getDatabySql(querySql);
//						if(null==queryList||queryList.size()==0){
//							String itemSql ="select t.t_itemname,t.t_jiaceleibie from gwm_item_flow t where t.id ="+itemId;
//							List itemList = itemDao.getDatabySql(itemSql);
//							if(null!=itemList&&itemList.size()>0&&null!=itemList.get(0)){
//								Object[] obj = (Object[])itemList.get(0);
//								if(null!=obj[0]){
//									itemName = obj[0].toString();
//								}
//								if(null!=obj[1]){
//									jianceleibie = obj[1].toString();
//								}
//							}
//							String biaozhuanSql ="select wmsys.wm_concat(t.t_jiancebiaozhun) from gwm_jiancebz_flow t where t.t_itemid ="+itemId;
//							List biaozhuanList = itemDao.getDatabySql(biaozhuanSql);
//							if(null!=biaozhuanList&&biaozhuanList.size()>0&&null!=biaozhuanList.get(0)){
//								jiancebiaozhun = biaozhuanList.get(0).toString();
//							}
//							String insertSql = "  insert into gwm_item_sample  " +
//									           " (id,t_samplenoid,t_sampleid, t_samplename, t_sampleno, t_itemid, t_itemname, t_jiaceleibie,t_jiancebiaozhun, t_deptid, t_deptleaderid, t_deptleadername, t_taskid, t_deptname,t_taskinfoid,ADDTIME) values  " +
//									           " (SEQ_GWM_ITEM_SAMPLE.Nextval,"+sampleNoId+","+sampleId+",'"+sampleName+"','"+sampleNo+"',"+itemId+",'"+itemName+"','"+jianceleibie+"','"+jiancebiaozhun+"',"+deptId+","+userid+",'"+username+"',"+taskId+",'"+deptName+"',"+flowtaskinfoid+",'"+DateUtilsAll.dateToStringShort(new Date())+"')";
//						    itemDao.executeSql(insertSql);
//							
//						}
//					}
//					
//				}
//			}
		}
	}
	
	/**
	 * 
	 * 描述：删除样品和项目
	 * @author wangxd
	 * @date Jul 15, 2015 11:42:21 AM
	 * @return void
	 * @throw
	 */
	public void deleteGwmItemAndSample(String ids,String taskId){
		String[] allIds = ids.split(",");
		for(int i=0;i<allIds.length;i++){
			String values = allIds[i].toString();  //样品编号表ID_基础表项目ID
			String[] msg = values.split("_");
			String sql = "delete from gwm_item_sample t where t.t_taskid  ="+taskId+" and t.t_sampleno = '"+msg[0]+"' and t.t_gmw_item_id="+msg[1];
			itemDao.executeSql(sql);
		}
		
		//重新计算费用总机
		//重新修改费用总计字段
		String itemMoneySql = "select sum(a.t_jiancemoney) from ( select distinct t.t_sampleno,t.t_gmw_item_id,t.t_jiancemoney  from gwm_item_sample t where t.t_taskid = "+taskId+" and t.t_jiancemoney is not null) a";
		List itemMoneyList = itemDao.getDatabySql(itemMoneySql);
		if(null!=itemMoneyList&&itemMoneyList.size()>0&&null!=itemMoneyList.get(0)){
			String  itemMoney = itemMoneyList.get(0).toString();
			String updateSql = "update gwm_changgijiance t  set t.t_feiyongzongji = "+itemMoney+" where t.id = (select f.main_id from flow_task f where f.id="+taskId+")";
			itemDao.executeSql(updateSql);
		}
		
		
	}
	/**
	 * 
	 * 描述：清空人员
	 * @author wangxd
	 * @date Jul 16, 2015 12:03:40 PM
	 * @return void
	 * @throw
	 */
	public void deleteUser(String ids,String taskInfoId){
		
		String[] allIds = ids.split(",");   
		for(int i=0;i<allIds.length;i++){
			String[] id = allIds[i].split("_");  //T_SAMPLENO+'_'+T_GMW_ITEM_ID
			String	sql = "update gwm_item_sample t set t.t_jianceyuanid='',t.t_jianceyuanname='' where   t.t_taskinfoid = "+taskInfoId+"  and t.t_sampleno='"+id[0]+"' and t.t_gmw_item_id="+id[1];
			itemDao.executeSql(sql);
		}
		
	}
	/**
	 * 
	 * 描述：分配用户
	 * @author wangxd
	 * @date Jul 16, 2015 12:09:55 PM
	 * @return void
	 * @throw
	 */
	public void fenpeiUser(String ids,String userId,String taskInfoId){
		String queryUserNameSql = "select wmsys.wm_concat(to_char(t.username))  from sys_users t where t.userid in ( "+userId+")";
		List list = itemDao.getDatabySql(queryUserNameSql);
		if(null!=list&&list.size()>0&&null!=list.get(0)){
			String username = list.get(0).toString();
			String[] allIds = ids.split(",");   
			for(int i=0;i<allIds.length;i++){
				String[] id = allIds[i].split("_");  //T_SAMPLENO+'_'+T_GMW_ITEM_ID
				String	updateSql = "update gwm_item_sample t set t.t_jianceyuanid=',"+userId+",',t.t_jianceyuanname='"+username+"' where  t.t_taskinfoid = "+taskInfoId+"  and t.t_sampleno='"+id[0]+"' and t.t_gmw_item_id="+id[1];
				itemDao.executeSql(updateSql);
			}
			
		}
	}
	/**
	 * 
	 * 描述：常规检测流程  调度审核委托单 修改检测标准
	 * @author wangxd
	 * @date Jul 24, 2015 9:27:56 AM
	 * @return void
	 * @throws IOException 
	 * @throw
	 */
	public void updateItemBiaozhun(String itemId,String taskId,String biaozhun,String jiancemoney){
		
		String updatesql = " update gwm_item_flow t  set t_jiancebiaozhun = '"+biaozhun+"',t.t_jiancemoney ='"+jiancemoney+"'  where t.t_taskid ="+taskId+" and t.t_itemid= "+itemId;
	    itemDao.executeSql(updatesql);
	}
	/**
	 * 我的请求---检测项目--整车和常规报告
	 * wxj
	 */
	public PageResult queryMyItemSample(Map filterMap){
		return itemDao.queryMyItemSample(filterMap);
	}
	/**
	 * 我的请求---研发报告
	 * wxj
	 */
	public PageResult queryMyItemSampleYF(Map filterMap){
		return itemDao.queryMyItemSampleYF(filterMap);
	}
	/**
	 * 
	 * 描述：检测项目管理 ：查询列表
	 * @author wangxd
	 * @date Sep 24, 2015 8:44:45 AM
	 * @return PageResult
	 * @throw
	 */
	public PageResult queryGwmItemList(Map filterMap)
	{
		return itemDao.queryGwmItemList(filterMap);
	}
	/**
	 * 
	 * 描述：检测项目列表删除功能
	 * @author wangxd
	 * @date Sep 24, 2015 2:16:15 PM
	 * @return void
	 * @throw
	 */
	public void deleteGwmItem(String ids,String flag){
		
		//删除同一个检测项目、检测单位的数据
		String[] allIds = ids.split(",");
		if("0".equals(flag)){
			for(int i=0;i<allIds.length;i++){
				String sql = "select t.t_itemname,t.t_jiancedanwei,t.t_jiancebumen,t.t_jiaceleibie from gwm_item t where t.id = "+allIds[i];
				List list = itemDao.getDatabySql(sql);
				if(null!=list&&list.size()>0){
					Object[] obj = (Object[])list.get(0);
					String itemName = obj[0]!=null?obj[0].toString():"";
					String danwei = obj[1]!=null?obj[1].toString():"";
					String bumen =  obj[2]!=null?obj[2].toString():"";
					String leibie =  obj[3]!=null?obj[3].toString():"";
					
					String deleteSql = "delete gwm_item t where t.t_itemname ='"+itemName+"' and t.t_jiancedanwei = '"+danwei+"' and t.t_jiancebumen='"+bumen+"' and t.t_jiaceleibie='"+leibie+"'";
					itemDao.executeSql(deleteSql);
				}
			}
		}
		else if("1".equals(flag)){
			for(int i=0;i<allIds.length;i++){
				String sql = "select t.t_itemname,t.t_jiancedanwei,t.t_jiancebumen,t.t_jiaceleibie from gwm_item t where t.id = "+allIds[i];
				List list = itemDao.getDatabySql(sql);
				if(null!=list&&list.size()>0){
					Object[] obj = (Object[])list.get(0);
					String itemName = obj[0]!=null?obj[0].toString():"";
					String danwei = obj[1]!=null?obj[1].toString():"";
					String bumen =  obj[2]!=null?obj[2].toString():"";
					String leibie =  obj[3]!=null?obj[3].toString():"";
					
					String tiaojian = "t.t_itemname='"+itemName+"' and t.t_jiancedanwei='"+danwei+"' and t.t_jiancebumen='"+bumen+"' and t.t_jiaceleibie='"+leibie+"'";
					
					//查询同一个检测项目不通检测参数的数据
					String canshuSql = "select distinct t.t_jiancecanshu from gwm_item t where "+tiaojian ;
					List canshulist = itemDao.getDatabySql(canshuSql);
					if(null!=canshulist&&canshulist.size()>0){
						   for(int j=0;j<canshulist.size();j++){
						    	String canshuName ="";
						    	if(null!= canshulist.get(j)){
						    		canshuName = " t_jiancecanshu ='"+canshulist.get(j).toString()+"'";
						    	}else{
						    		canshuName = " t_jiancecanshu is null ";
						    	}
						    	String sql1 = "select t.id from gwm_item t where "+tiaojian+"  and "+canshuName+" and t.id in ("+ids+") order by t.id asc ";
						    	List list1 = itemDao.getDatabySql(sql1);
						    	if(null!=list1&&list1.size()>0&&null!=list1.get(0)){
						    		String deleteSql = "delete gwm_item t1 where t1.id in (select t.id from gwm_item t where "+tiaojian+"  and "+canshuName +" and t.id in ("+ids+") )  and t1.id <> "+list1.get(0);
						    	    itemDao.executeSql(deleteSql);
						    	}
						    }
					}
				}
			}
			String deleteSql = "update  gwm_item t set t.t_jiancebiaozhun='' where t.id in ("+ids+")";
			itemDao.executeSql(deleteSql);
		}
		
	}
	/**
	 * 
	 * 描述：删除原始记录
	 * @author wangxd
	 * @date Oct 11, 2015 12:30:31 PM
	 * @return void
	 * @throw
	 */
	public void delYsjl(String ids){
		String idss[] = ids.split(",");
		for(int i=0;i<idss.length;i++){
			String querySql = "select distinct t.t_taskid,t.t_taskinfoid,t.t_ysjlfilepath from gwm_item_sample t where t.id="+idss[i] +" and t.t_ysjlfilepath is not null";
			List queryList = itemDao.getDatabySql(querySql);
			if(null!=queryList&&queryList.size()>0){
				Object[] obj = (Object[])queryList.get(0);
				String taskId = obj[0]!=null?obj[0].toString():"";
				String taskInfoId = obj[1]!=null?obj[1].toString():"";
				String ysjlpath = obj[2]!=null?obj[2].toString():"";
				
				//清空同一个份原始记录单对应的数据
				String updateSql = "update gwm_item_sample t set " +
						           " t.t_ysjlfilepath='',t.t_ysjlannexname='',t.t_filemodeid='',t.t_mobantype=''," +
						           " t.t_result='',t.t_result1='',t.t_result2='',t.t_result3=''," +
						           " t.t_ysjlsfedit =null,t_shiyanqzsj='',t_shiyandd='',t_shiyantt=''," +
						           " t.t_ysjleditor='',t.t_sfdianziqianm =null where t.t_ysjlfilepath='"+ysjlpath+"' and t.t_taskid="+taskId+" and t.t_taskinfoid = "+taskInfoId;
			   itemDao.executeSql(updateSql);
			}
		}
	}
	/**
	 * 常规检测流程 ----填写原始记录节点  删除、恢复检测标准
	 */
	public void deleteJiancecanshu(String flag,String ids) {
		Long sfdelete = null;
		if("0".equals(flag)){
			sfdelete = 0l;
		}
		String updateSql =" update gwm_item_sample t set t.t_sfdelete="+sfdelete+"  where t.id in ("+ids+")";
		itemDao.executeSql(updateSql);
	}
	
	/**
	 * 
	 * 描述：查询已删除检测参数列表
	 * @author wangxd
	 * @date Oct 13, 2015 10:23:53 PM
	 * @return PageResult
	 * @throw
	 */
	public PageResult queryJiancecanshuList(Map filterMap)
	{
		return itemDao.queryJiancecanshuList(filterMap);
	}
	/**
	 * 
	 * 描述：检测项目管理中 --
	 * @author wangxd
	 * @date Oct 15, 2015 10:21:24 AM
	 * @return PageResult
	 * @throw
	 */
	public PageResult queryItemcanshuList(Map filterMap) 
	{
		return itemDao.queryItemcanshuList(filterMap);
	}
	/**
	 * 
	 * 描述：检测项目管理----删除检测参数
	 * @author wangxd
	 * @date Oct 15, 2015 9:37:28 PM
	 * @return void
	 * @throw
	 */
	public void deleteGwmItemCanshu(String itemname,String danwei,String leibie,String canshu) throws IOException{
		
		String tiaojian = " t.t_itemname='"+itemname+"' and t.t_jiancedanwei='"+danwei+"' and t.t_jiaceleibie='"+leibie+"'";   //确定同一个检测项目
		String[] allCanshu = canshu.split(",");
		for(int i=0;i<allCanshu.length;i++){
			//查询出同一个项目中 同一检测参数对应的不通标准
			String querysql = "select distinct t.t_jiancebiaozhun from gwm_item t where "+tiaojian+" and (t.t_jiancecanshu is null or t.t_jiancecanshu ='"+allCanshu[i]+"')";
		    List biaozhunlist = itemDao.getDatabySql(querysql);
			if(null!=biaozhunlist&&biaozhunlist.size()>0){
				
			    //循环判断 参数为空  检测标准相同的数据  如果数据条数>0  删除其他数据 只留一条数据
			    for(int j=0;j<biaozhunlist.size();j++){
			    	String biaozhunName ="";
			    	if(null!= biaozhunlist.get(j)){
			    		biaozhunName = " t_jiancebiaozhun ='"+biaozhunlist.get(j).toString()+"'";
			    	}else{
			    		biaozhunName = " t_jiancebiaozhun is null ";
			    	}
			    	String sql1 = "select t.id from gwm_item t where "+tiaojian+"  and (t.t_jiancecanshu is null or t.t_jiancecanshu ='"+allCanshu[i]+"') and "+biaozhunName+" order by t.id asc ";
			    	List list1 = itemDao.getDatabySql(sql1);
			    	if(null!=list1&&list1.size()>0&&null!=list1.get(0)){
			    		String deleteSql = "delete gwm_item t1 where t1.id in (select t.id from gwm_item t where "+tiaojian+"  and (t.t_jiancecanshu is null or t.t_jiancecanshu ='"+allCanshu[i]+"') and "+biaozhunName +")  and t1.id <> "+list1.get(0);
			    	    itemDao.executeSql(deleteSql);
			    	}
			    }
			}
			 //修改检测参数参数为空
			String updateSql = " update gwm_item t set t.t_jiancecanshu='' where "+tiaojian+" and t.t_jiancecanshu ='"+allCanshu[i]+"'";
		    itemDao.executeSql(updateSql);
			
		}
	}
	
	
	
	public String executeSql(String sql){
		return itemDao.executeSql(sql);
	}
	public List getDatabySql(String sql) {
		return itemDao.getDatabySql(sql);
	}
	public String queryDeptUsers(String deptid) 
	{
		return itemDao.queryDeptUsers(deptid);
	}
	public IItemDao getItemDao() {
		return itemDao;
	}

	public void setItemDao(IItemDao itemDao) {
		this.itemDao = itemDao;
	}
    
	
	
}
